/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.exceptions.NonexistentEntityException;
import dao.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import modelo.Evaluador;
import modelo.ExperienciaLaboral;
import modelo.ExperienciaLaboralPK;

/**
 *
 * @author Fabian
 */
public class ExperienciaLaboralJpaController implements Serializable {

    public ExperienciaLaboralJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(ExperienciaLaboral experienciaLaboral) throws PreexistingEntityException, Exception {
        if (experienciaLaboral.getExperienciaLaboralPK() == null) {
            experienciaLaboral.setExperienciaLaboralPK(new ExperienciaLaboralPK());
        }
        experienciaLaboral.getExperienciaLaboralPK().setCedulaEvaluador(experienciaLaboral.getEvaluador().getCedula());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Evaluador evaluador = experienciaLaboral.getEvaluador();
            if (evaluador != null) {
                evaluador = em.getReference(evaluador.getClass(), evaluador.getCedula());
                experienciaLaboral.setEvaluador(evaluador);
            }
            em.persist(experienciaLaboral);
            if (evaluador != null) {
                evaluador.getExperienciaLaboralList().add(experienciaLaboral);
                evaluador = em.merge(evaluador);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findExperienciaLaboral(experienciaLaboral.getExperienciaLaboralPK()) != null) {
                throw new PreexistingEntityException("ExperienciaLaboral " + experienciaLaboral + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(ExperienciaLaboral experienciaLaboral) throws NonexistentEntityException, Exception {
        experienciaLaboral.getExperienciaLaboralPK().setCedulaEvaluador(experienciaLaboral.getEvaluador().getCedula());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ExperienciaLaboral persistentExperienciaLaboral = em.find(ExperienciaLaboral.class, experienciaLaboral.getExperienciaLaboralPK());
            Evaluador evaluadorOld = persistentExperienciaLaboral.getEvaluador();
            Evaluador evaluadorNew = experienciaLaboral.getEvaluador();
            if (evaluadorNew != null) {
                evaluadorNew = em.getReference(evaluadorNew.getClass(), evaluadorNew.getCedula());
                experienciaLaboral.setEvaluador(evaluadorNew);
            }
            experienciaLaboral = em.merge(experienciaLaboral);
            if (evaluadorOld != null && !evaluadorOld.equals(evaluadorNew)) {
                evaluadorOld.getExperienciaLaboralList().remove(experienciaLaboral);
                evaluadorOld = em.merge(evaluadorOld);
            }
            if (evaluadorNew != null && !evaluadorNew.equals(evaluadorOld)) {
                evaluadorNew.getExperienciaLaboralList().add(experienciaLaboral);
                evaluadorNew = em.merge(evaluadorNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                ExperienciaLaboralPK id = experienciaLaboral.getExperienciaLaboralPK();
                if (findExperienciaLaboral(id) == null) {
                    throw new NonexistentEntityException("The experienciaLaboral with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(ExperienciaLaboralPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ExperienciaLaboral experienciaLaboral;
            try {
                experienciaLaboral = em.getReference(ExperienciaLaboral.class, id);
                experienciaLaboral.getExperienciaLaboralPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The experienciaLaboral with id " + id + " no longer exists.", enfe);
            }
            Evaluador evaluador = experienciaLaboral.getEvaluador();
            if (evaluador != null) {
                evaluador.getExperienciaLaboralList().remove(experienciaLaboral);
                evaluador = em.merge(evaluador);
            }
            em.remove(experienciaLaboral);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<ExperienciaLaboral> findExperienciaLaboralEntities() {
        return findExperienciaLaboralEntities(true, -1, -1);
    }

    public List<ExperienciaLaboral> findExperienciaLaboralEntities(int maxResults, int firstResult) {
        return findExperienciaLaboralEntities(false, maxResults, firstResult);
    }

    private List<ExperienciaLaboral> findExperienciaLaboralEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(ExperienciaLaboral.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public ExperienciaLaboral findExperienciaLaboral(ExperienciaLaboralPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ExperienciaLaboral.class, id);
        } finally {
            em.close();
        }
    }

    public int getExperienciaLaboralCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<ExperienciaLaboral> rt = cq.from(ExperienciaLaboral.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
